/**
 * @author Ahammer
 * @date   2009 06
 * @update 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;
import main.IqmTools;
import main.Tank;

public class IqmStatisticsGUI extends IqmOperatorJFrame 
							  implements IqmOperatorGUIBuilder, ActionListener, ChangeListener {

	private  ParameterBlockJAI     pbJAI       = null;  
	//public  ImgProcFuncJFrame    opGUIFrame  = null;
	//private  IqmStatisticsGUI    opGUIFrame  = null;  //actionPerformed events werden in dieser Klasse abgehandelt

	private  JCheckBox 	    jCheckBoxBinary	 = null;
	private  JCheckBox 	    jCheckBoxOrder1	 = null;
	private  JCheckBox 	    jCheckBoxOrder2	 = null;
	
	private JPanel 			jPanelDistance	  = null;	
	private JLabel			jLabelDistance	  = null;
	private JSpinner	    jSpinnerDistance  = null;

	private JPanel 			jPanelRange	           = null;	//Grey level Range
	private JLabel			jLabelRange	           = null;
	private JLabel			jLabelValueRange	   = null;
	private JSpinner	    jSpinnerRange          = null;
	
	private JRadioButtonMenuItem  buttVert            = null;	
	private JRadioButtonMenuItem  buttHoriz           = null;	
	private JRadioButtonMenuItem  buttPlus            = null;
	private JRadioButtonMenuItem  buttX               = null;
	private JRadioButtonMenuItem  buttAll             = null;
	private JRadioButtonMenuItem  buttDirectionHidden = null;
	private JToolBar              jToolBarDirection   = null;
	private ButtonGroup           buttGroupDirection  = null;
	
	private JRadioButtonMenuItem  buttIncl             = null;
	private JRadioButtonMenuItem  buttExcl             = null;	
	private JRadioButtonMenuItem  buttBackgroundHidden = null;
	private JToolBar              jToolBarBackground   = null;
	private ButtonGroup           buttGroupBackground  = null;
	private JPanel			      jPanelBackground     = null;
	private	JLabel				  jLabelBackground	   = null;
	
	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pbJAI
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (jCheckBoxBinary.isSelected())    pbJAI.setParameter("Binary", 1);
		if (!jCheckBoxBinary.isSelected())   pbJAI.setParameter("Binary", 0);		
		if (jCheckBoxOrder1.isSelected())    pbJAI.setParameter("Order1", 1);
		if (!jCheckBoxOrder1.isSelected())   pbJAI.setParameter("Order1", 0);		
		if (jCheckBoxOrder2.isSelected())    pbJAI.setParameter("Order2", 1);
		if (!jCheckBoxOrder2.isSelected())   pbJAI.setParameter("Order2", 0);
		
		pbJAI.setParameter("Distance",  ((Number)jSpinnerDistance.getValue()).intValue());
		pbJAI.setParameter("Range",     ((Number)jSpinnerRange.getValue()).intValue());
		
		if (buttVert.isSelected())  pbJAI.setParameter("Direction", 0);
		if (buttHoriz.isSelected()) pbJAI.setParameter("Direction", 1);
		if (buttPlus.isSelected())  pbJAI.setParameter("Direction", 2);
		if (buttX.isSelected())     pbJAI.setParameter("Direction", 3);
		if (buttAll.isSelected())   pbJAI.setParameter("Direction", 4);
		
		if (buttIncl.isSelected()) pbJAI.setParameter("Background", 0);
		if (buttExcl.isSelected()) pbJAI.setParameter("Background", 1);
	    
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmStatisticsGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
//      String type = IqmTools.getCurrImgTyp(pi);
	       
		if (pbJAI.getIntParameter("Binary")  == 0) jCheckBoxBinary.setSelected(false);
		if (pbJAI.getIntParameter("Binary")  == 1) jCheckBoxBinary.setSelected(true);
		if (pbJAI.getIntParameter("Order1")  == 0) jCheckBoxOrder1.setSelected(false);
		if (pbJAI.getIntParameter("Order1")  == 1) jCheckBoxOrder1.setSelected(true);
	    if (pbJAI.getIntParameter("Order2")  == 0) jCheckBoxOrder2.setSelected(false);
	    if (pbJAI.getIntParameter("Order2")  == 1) jCheckBoxOrder2.setSelected(true);  
		 
	    jSpinnerDistance.removeChangeListener(this);	
	    jSpinnerDistance.setValue(pbJAI.getIntParameter("Distance"));
	    jSpinnerDistance.addChangeListener(this);		

	    jSpinnerRange.removeChangeListener(this);	
	    jSpinnerRange.setValue(pbJAI.getIntParameter("Range"));
	    jSpinnerRange.addChangeListener(this);		
	    
	    buttDirectionHidden.setSelected(true);
		buttBackgroundHidden.setSelected(true);	
		if (pbJAI.getIntParameter("Direction") == 0)  buttVert.setSelected(true);
		if (pbJAI.getIntParameter("Direction") == 1)  buttHoriz.setSelected(true);
		if (pbJAI.getIntParameter("Direction") == 2)  buttPlus.setSelected(true);
		if (pbJAI.getIntParameter("Direction") == 3)  buttX.setSelected(true);
		if (pbJAI.getIntParameter("Direction") == 4)  buttAll.setSelected(true);
		
		if (pbJAI.getIntParameter("Background") == 0)  buttIncl.setSelected(true);
		if (pbJAI.getIntParameter("Background") == 1)  buttExcl.setSelected(true);
		
		if (jCheckBoxOrder2.isSelected()){
			setOrder2OptionsStatus(true);			
		}else{
			setOrder2OptionsStatus(false);		
		}
		int range  = ((Number)jSpinnerRange.getValue()).intValue();	
		int value = 1;
		for (int i = 1; i <= range; i++){
			value = value * 2;
		}
		jLabelValueRange.setText("Grey Values: "+String.valueOf(value));
		
	}
	//-------------------------------------------------------------------------------------------------
	/**
	 * This method enables or disables the 2nd order options
	 */
	private void setOrder2OptionsStatus(boolean b){
		if (b){
			jLabelDistance.setEnabled(true);
			jSpinnerDistance.setEnabled(true);
			jLabelRange.setEnabled(true);
			jSpinnerRange.setEnabled(true);
			jLabelValueRange.setEnabled(true);
			jToolBarDirection.setEnabled(true);	
			buttVert.setEnabled(true);
			buttHoriz.setEnabled(true);	
			buttPlus.setEnabled(true);
			buttX.setEnabled(true);
			buttAll.setEnabled(true);
		}else{
			jLabelDistance.setEnabled(false);
			jSpinnerDistance.setEnabled(false);
			jLabelRange.setEnabled(false);
			jSpinnerRange.setEnabled(false);
			jLabelValueRange.setEnabled(false);
			jToolBarDirection.setEnabled(false);	
			buttVert.setEnabled(false);
			buttHoriz.setEnabled(false);	
			buttPlus.setEnabled(false);
			buttX.setEnabled(false);
			buttAll.setEnabled(false);
		}
		
	}
	/**
	 * constructor
	 */
	public IqmStatisticsGUI() {

	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmStatisticsGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("Statistics");  
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+150);
		this.getJContentPane().add(getJCheckBoxBinary(),   getGridBagConstraintsBinary());	
		this.getJContentPane().add(getJCheckBoxOrder1(),   getGridBagConstraintsOrder1());	
		this.getJContentPane().add(getJCheckBoxOrder2(),   getGridBagConstraintsOrder2());	
		this.getJContentPane().add(getJToolBarDirection(), getGridBagConstraintsButtonGroupDirection());
		this.getJContentPane().add(getJPanelDistance(),    getGridBagConstraintsDistance());
		this.getJContentPane().add(getJPanelRange(),       getGridBagConstraintsRange());
		this.getJContentPane().add(getJPanelBackground(),  getGridBagConstraintsBackground());

		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmStatistics");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmStatistics"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		this.setParameterValues();
		this.setParameterBlock();
		
		this.pack();
	}
	
	private  GridBagConstraints getGridBagConstraintsBinary(){
		GridBagConstraints gridBagConstraintsBinary = new GridBagConstraints();
		gridBagConstraintsBinary.gridx = 1;
		gridBagConstraintsBinary.gridy = 4;
		gridBagConstraintsBinary.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsBinary.insets = new Insets(10,20,0,0); //top left bottom right
		//gridBagConstraintsBinary.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsBinary;
	}
	private  GridBagConstraints getGridBagConstraintsOrder1(){
		GridBagConstraints gridBagConstraintsOrder1 = new GridBagConstraints();
		gridBagConstraintsOrder1.gridx = 2;
		gridBagConstraintsOrder1.gridy = 4;
		gridBagConstraintsOrder1.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsOrder1.insets = new Insets(10,0,0,0); //top left bottom right
		//gridBagConstraintsOrder1.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOrder1;
	}
	private  GridBagConstraints getGridBagConstraintsOrder2(){
		GridBagConstraints gridBagConstraintsOrder2 = new GridBagConstraints();
		gridBagConstraintsOrder2.gridx = 3;
		gridBagConstraintsOrder2.gridy = 4;
		gridBagConstraintsOrder2.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsOrder2.insets = new Insets(10,0,0,0); //top left bottom right
		//gridBagConstraintsOrder2.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOrder2;
	}
	private  GridBagConstraints getGridBagConstraintsRange(){
		GridBagConstraints gridBagConstraintsRange = new GridBagConstraints();
		gridBagConstraintsRange.gridx = 1;
		gridBagConstraintsRange.gridy = 6;
		gridBagConstraintsRange.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsRange.insets = new Insets(30,0,0,0); //top left bottom right
		//gridBagConstraintsRange.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRange;
	}
	private  GridBagConstraints getGridBagConstraintsDistance(){
		GridBagConstraints gridBagConstraintsDistance = new GridBagConstraints();
		gridBagConstraintsDistance.gridx = 1;
		gridBagConstraintsDistance.gridy = 7;
		gridBagConstraintsDistance.gridwidth = 1;//?
		gridBagConstraintsDistance.insets = new Insets(10,0,0,0);  //top left bottom right
		//gridBagConstraintsDistance.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsDistance;
	}
	private  GridBagConstraints getGridBagConstraintsButtonGroupDirection(){
		GridBagConstraints gridBagConstraintsButtonDirectionGroup = new GridBagConstraints();
		gridBagConstraintsButtonDirectionGroup.gridx = 2;
		gridBagConstraintsButtonDirectionGroup.gridy = 6;
		gridBagConstraintsButtonDirectionGroup.gridwidth = 2;   //
		gridBagConstraintsButtonDirectionGroup.gridheight = 2;  //
		gridBagConstraintsButtonDirectionGroup.insets = new Insets(0,0,0,0);  //top left bottom right
		//gridBagConstraintsButtonDirectionGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonDirectionGroup;
	}
	
	private  GridBagConstraints getGridBagConstraintsBackground(){
		GridBagConstraints gridBagConstraintsButtonBackgroundGroup = new GridBagConstraints();
		gridBagConstraintsButtonBackgroundGroup.gridx = 1;
		gridBagConstraintsButtonBackgroundGroup.gridy = 8;
		gridBagConstraintsButtonBackgroundGroup.gridwidth = 3;//?
		gridBagConstraintsButtonBackgroundGroup.insets = new Insets(20,0,20,0);  //top left bottom right
		//gridBagConstraintsButtonBackgroundGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonBackgroundGroup;
	}
	//------------------------------------------------------------------------------------------------------

	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
	
	}
	//------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jCheckBoxBinary	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxBinary() {
		if (jCheckBoxBinary == null) {
			jCheckBoxBinary = new JCheckBox();
			jCheckBoxBinary.setText("Binary");
			jCheckBoxBinary.setToolTipText("Calculates binary features such as total pixel number, number of pixels greater than 0, ......");
			jCheckBoxBinary.addActionListener(this);			
			jCheckBoxBinary.setActionCommand("parameter");			
		}
		return jCheckBoxBinary;
	}
	/**
	 * This method initializes jCheckBoxOrder1	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxOrder1() {
		if (jCheckBoxOrder1 == null) {
			jCheckBoxOrder1 = new JCheckBox();
			jCheckBoxOrder1.setText("1st Order");
			jCheckBoxOrder1.setToolTipText("Calculates 1st order features such as Min, Max, Mean, Energy, Entropy, Skewness, Kurtosis,....");
			jCheckBoxOrder1.addActionListener(this);			
			jCheckBoxOrder1.setActionCommand("parameter");			
		}
		return jCheckBoxOrder1;
	}
	/**
	 * This method initializes jCheckBoxOrder2	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxOrder2() {
		if (jCheckBoxOrder2 == null) {
			jCheckBoxOrder2 = new JCheckBox();
			jCheckBoxOrder2.setText("2nd Order");
			jCheckBoxOrder2.setToolTipText("Calculates 2nd order features using the co-occurance matrix");
			jCheckBoxOrder2.addActionListener(this);			
			jCheckBoxOrder2.setActionCommand("parameter");		
			jCheckBoxOrder2.setEnabled(true);
		}
		return jCheckBoxOrder2;
	}

	//----------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelDistance	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelDistance() {
		if (jPanelDistance == null) {
			jPanelDistance = new JPanel();
			jPanelDistance.setLayout(new BorderLayout());		
			jLabelDistance = new JLabel("Distance: ");
			jLabelDistance.setPreferredSize(new Dimension(75, 22));
			jLabelDistance.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerDistance = new JSpinner(sModel);
			jSpinnerDistance.setPreferredSize(new Dimension(60, 22));
			jSpinnerDistance.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerDistance.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelDistance.add(jLabelDistance, 	 BorderLayout.WEST);
			jPanelDistance.add(jSpinnerDistance, BorderLayout.CENTER);			
		}
		return jPanelDistance;
	}
	/**
	 * This method initializes jJPanelRange	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRange() {
		if (jPanelRange == null) {
			jPanelRange = new JPanel();
			jPanelRange.setLayout(new BorderLayout());		
			jLabelRange = new JLabel("Range: ");
			jLabelRange.setPreferredSize(new Dimension(75, 22));
			jLabelRange.setHorizontalAlignment(JLabel.RIGHT);
			jLabelValueRange = new JLabel("Value: ");
			jLabelValueRange.setPreferredSize(new Dimension(75, 22));
			jLabelValueRange.setHorizontalAlignment(JLabel.CENTER);

			SpinnerModel sModel = new SpinnerNumberModel(5, 1, 8, 1); //init, min, max, step  range = 2^n   n= 1,....,8
			jSpinnerRange = new JSpinner(sModel);
			jSpinnerRange.setPreferredSize(new Dimension(60, 22));
			jSpinnerRange.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRange.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelRange.add(jLabelRange, 	    BorderLayout.WEST);
			jPanelRange.add(jSpinnerRange,      BorderLayout.CENTER);			
			jPanelRange.add(jLabelValueRange, BorderLayout.SOUTH);			
		}
		return jPanelRange;
	}
	
	
	//-------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option:	vertical Direction
	 * 
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttVert() {
		//if (buttVert == null) {
			buttVert  = new JRadioButtonMenuItem();
			buttVert .setText("Vertical");
			buttVert .setToolTipText("uses the vertical pixels");
			buttVert .addActionListener(this);
			buttVert.setActionCommand("parameter");
		//}
		return buttVert;
	}
	/**
	 * This method initializes the Option: horizontal Direction
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttHoriz() {
		//if (buttHoriz == null) {
			buttHoriz  = new JRadioButtonMenuItem();
			buttHoriz .setText("Horizontal");
			buttHoriz .setToolTipText("uses the horizontal pixels");
			buttHoriz .addActionListener(this);
			buttHoriz.setActionCommand("parameter");
		//}
		return buttHoriz;
	}
	/**
	 * This method initializes the Option: Plus Direction	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttPlus() {
		//if (buttPlus == null) {
			buttPlus  = new JRadioButtonMenuItem();
			buttPlus .setText("+");
			buttPlus .setToolTipText("uses the vertical and horizontal pixels");
			buttPlus .addActionListener(this);
			buttPlus.setActionCommand("parameter");
		//}
		return buttPlus;
	}
	/**
	 * This method initializes the Option: X shaped Direction
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttX() {
		//if (buttX == null) {
			buttX  = new JRadioButtonMenuItem();
			buttX .setText("X shaped");
			buttX .setToolTipText("uses the diagonal pixels");
			buttX .addActionListener(this);
			buttX.setActionCommand("parameter");
		//}
		return buttX;
	}
	/**
	 * This method initializes the Option: all Directions
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttAll() {
		//if (buttAll == null) {
			buttAll  = new JRadioButtonMenuItem();
			buttAll .setText("All");
			buttAll .setToolTipText("uses all neighboring pixels");
			buttAll .addActionListener(this);
			buttAll.setActionCommand("parameter");
		//}
		return buttAll;
	}
	
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtDirectionHidden() {
		//if (buttDirectionHidden == null) {
			buttDirectionHidden  = new JRadioButtonMenuItem();
			buttDirectionHidden .setText("Hidden");
			buttDirectionHidden.setVisible(false);
		//}
		return buttDirectionHidden;
	}


	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarDirection() {
//		if (jToolBarDirection== null) {
			jToolBarDirection = new JToolBar("Options");
			jToolBarDirection.setOrientation(1); // 0 Horizontal 1 Vertical Direction
			jToolBarDirection.add(getJRadioButtonMenubuttVert());
			jToolBarDirection.add(getJRadioButtonMenubuttHoriz());
			jToolBarDirection.add(getJRadioButtonMenubuttPlus());
			jToolBarDirection.add(getJRadioButtonMenubuttX());
			jToolBarDirection.add(getJRadioButtonMenubuttAll());
			jToolBarDirection.add(getJRadioButtonMenuButtDirectionHidden());
			//jToolBarDirection.addSeparator();
			this.setButtonGroupDirection();	//Gruppieren der JRadioButtons
//		}
		return jToolBarDirection;
	}

	private  void setButtonGroupDirection() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupDirection = new ButtonGroup();
			buttGroupDirection.add(buttVert);
			buttGroupDirection.add(buttHoriz);
			buttGroupDirection.add(buttPlus);
			buttGroupDirection.add(buttX);
			buttGroupDirection.add(buttAll);
			buttGroupDirection.add(buttDirectionHidden);
	}

	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttIncl() {
		//if (buttIncl == null) {
			buttIncl  = new JRadioButtonMenuItem();
			buttIncl .setText("Inclusive Background");
			buttIncl .setToolTipText("statistics of all pixel values");
			buttIncl .addActionListener(this);
			buttIncl.setActionCommand("parameter");
		//}
		return buttIncl;
	}
	/**
	 * This method initializes the Option:
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenubuttExcl() {
		//if (buttExcl == null) {
			buttExcl  = new JRadioButtonMenuItem();
			buttExcl .setText("Exclusive Background");
			buttExcl .setToolTipText("statistics without zero pixel values");
			buttExcl .addActionListener(this);
			buttExcl.setActionCommand("parameter");
			buttExcl.setEnabled(true);
		//}
		return buttExcl;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBackgroundHidden() {
		//if (buttBackgroundHidden == null) {
			buttBackgroundHidden  = new JRadioButtonMenuItem();
			buttBackgroundHidden .setText("Hidden");
			buttBackgroundHidden.setVisible(false);
		//}
		return buttBackgroundHidden;
	}


//	/**
//	 * This method initializes jJToolBarBar	
//	 * 	
//	 * @return javax.swing.JToolBar	
//	 */
//	private   JToolBar getJToolBarBackground() {
////		if (jToolBarBackground== null) {
//			jToolBarBackground = new JToolBar("Options");
//			jToolBarBackground.setOrientation(1); // 0 Horizontal 1 Vertical orientation
//			jToolBarBackground.add(getJRadioButtonMenubuttIncl());
//			jToolBarBackground.add(getJRadioButtonMenubuttExcl());
//			jToolBarBackground.add(getJRadioButtonMenuButtBackgroundHidden());
//			//jToolBarBackground.addSeparator();
//			this.setButtonGroupBackground();	//Gruppieren der JRadioButtons
////		}
//		return jToolBarBackground;
//	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JPanel getJPanelBackground() {
//		if (jToolBarBackground== null) {
			jPanelBackground = new JPanel();
			jPanelBackground.setLayout(new BorderLayout());	
			jLabelBackground = new JLabel("Calculation Option : ");
			jToolBarBackground = new JToolBar("Options");
			jToolBarBackground.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarBackground.add(getJRadioButtonMenubuttIncl());
			jToolBarBackground.add(getJRadioButtonMenubuttExcl());
			jToolBarBackground.add(getJRadioButtonMenuButtBackgroundHidden());
			//jToolBarBackground.addSeparator();
			this.setButtonGroupBackground();	//Gruppieren der JRadioButtons
			jPanelBackground.add(jLabelBackground, 	 BorderLayout.WEST);
			jPanelBackground.add(jToolBarBackground, BorderLayout.CENTER);			
//		}
		return jPanelBackground;
	}


	private void setButtonGroupBackground() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupBackground = new ButtonGroup();
			buttGroupBackground.add(buttIncl);
			buttGroupBackground.add(buttExcl);
			buttGroupBackground.add(buttBackgroundHidden);
	}
    //----------------------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmStatisticsGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmStatistics".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {		
			if(buttVert == e.getSource()){
			}
			if(buttHoriz  == e.getSource()){	
			}
			if(buttPlus  == e.getSource()){			
			}
			if(buttX  == e.getSource()){			
			}
			if(buttAll  == e.getSource()){		
			}
			if(jCheckBoxOrder2  == e.getSource()){
				if (jCheckBoxOrder2.isSelected()){
					this.setOrder2OptionsStatus(true);	
				}else{
					this.setOrder2OptionsStatus(false);		
				}
				
			}
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		int distance  = ((Number)jSpinnerDistance.getValue()).intValue();
			
		if (jSpinnerDistance  == e.getSource()) {
				
		}
		int range  = ((Number)jSpinnerRange.getValue()).intValue();
		
		if (jSpinnerRange  == e.getSource()) {
			int value = 1;
			for (int i = 1; i <= range; i++){
				value = value * 2;
			}
			jLabelValueRange.setText("Grey Values: "+String.valueOf(value));
		}
		this.setParameterBlock();
		this.setParameterValues();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmStatisticsGUI gui = new IqmStatisticsGUI();
			    gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
